//
//  ST_DTChatRepacketCell.swift
//  btvideo
//
//  Created by mac on 2020/10/19.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_DTChatRepacketCell: ST_DTChatCell {
    
    
    fileprivate let normal_image_name = "redPacketBackground_icon"
    
    
    fileprivate let disable_image_name = "红包不可用_icon"
    
    fileprivate lazy var red_packet_image_view:UIImageView = {
        let control = UIImageView()
          control.contentMode = .scaleAspectFill
        control.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
        control.addGestureRecognizer(tap)
          return control
      }()
    
    
    fileprivate lazy var red_packet_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#FFD97F")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    override var message: ST_DTChatMessage!{
        didSet{
            
            icon.pin_setImage(from: message.portraitUri.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
                
            self.uuid = message.uuid
                
            guard let attach = message.customAttachment255 else { return  }
            
            if message.redPacketStatus == .normal{
                red_packet_image_view.image = UIImage.init(named: normal_image_name)
                red_packet_label.text = attach.redPacketTxt
            }else{
                
                if message.redPacketStatus == .expire{
                    red_packet_label.text = "红包已过期"
                }else if message.redPacketStatus == .received{
                    red_packet_label.text = "红包已领取"
                }
                red_packet_image_view.image = UIImage.init(named: disable_image_name)
            }
        }
    }
    
    var received_red_packet_closure:((ST_DTChatMessage)->())?
    
    @objc fileprivate func tapAction(){
        
        self.received_red_packet_closure?(message)
    }
    
}


//MARK: - Left
class ST_DTChatRepacketLeftCell: ST_DTChatRepacketCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func commonInit() {
        super.commonInit()
        
        contentView.addSubview(red_packet_image_view)
        red_packet_image_view.addSubview(red_packet_label)
     
        icon.snp.makeConstraints { (make) in
            make.width.height.equalTo(DTChatUserImageWidth)
            make.left.top.equalToSuperview().offset(15)
        }
        
        
        red_packet_image_view.snp.makeConstraints { (make) in
            make.width.equalTo(110)
            make.height.equalTo(150)
            make.left.equalTo(icon.snp.right).offset(10)
            make.top.equalTo(icon)
        }
        
        red_packet_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(9)
            make.right.equalToSuperview().offset(-9)
            make.top.equalToSuperview().offset(87)
        }
        
        
        
    }
    
    
    
    
}

//MARK: - right
class ST_DTChatRepacketRightCell: ST_DTChatRepacketCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func commonInit() {
        super.commonInit()
        
        icon.removeFromSuperview()
        icon_supplement_image_view.removeFromSuperview()
        
        contentView.addSubview(red_packet_image_view)
        red_packet_image_view.addSubview(red_packet_label)
     
        red_packet_image_view.snp.makeConstraints { (make) in
            make.width.equalTo(110)
            make.height.equalTo(150)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(15)
        }
        
        red_packet_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(9)
            make.right.equalToSuperview().offset(-9)
            make.top.equalToSuperview().offset(87)
        }
        
        
        contentView.addSubview(upload_error_btn)
        upload_error_btn.snp.makeConstraints { (make) in
            make.centerY.equalTo(red_packet_image_view)
            make.right.equalTo(red_packet_image_view.snp.left).offset(-8)
            make.width.height.equalTo(16)
        }
        
        contentView.addSubview(uploadIndicator)
        uploadIndicator.snp.makeConstraints { (make) in
            make.centerY.equalTo(red_packet_image_view)
            make.right.equalTo(red_packet_image_view.snp.left).offset(-8)
        }
    }
    
   
    override func setting_uplaod_status() {
        
        if message.uploadStatus == .uploading {
            
            uploadIndicator.startAnimating()
            
        }else{
            uploadIndicator.stopAnimating()
        }
        
        upload_error_btn.isHidden = message.uploadStatus != .failure
    }
    
}


//MARK: - 领取了
class ST_DTChatReceivedRepacketCell: ST_DTChatCell {
    
    
//    Cell Height 17 + 15
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var received_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#ACB4C0")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    override func commonInit() {
        super.commonInit()
        
        icon.removeFromSuperview()
        icon_supplement_image_view.removeFromSuperview()
        
        contentView.addSubview(received_label)
        received_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
        }
    }
    
    
    override var message: ST_DTChatMessage!{
        didSet{
            
            self.uuid = message.uuid
                
//            guard let attach = message.customAttachment255 else { return  }
            
            if message.direction == .receive{
                received_label.text = "对方领取了你的红包"
            }else{
                received_label.text = "已领取对方的红包"
            }
            
        }
    }
    
}
